package com.controller;

import com.api.CommonResult;
import com.entity.table.CultureTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Culture;
import com.service.CultureService;

import java.io.Serializable;
import java.util.List;

/**
 * 咖啡及文化 控制层。
 *
 * @author admin
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/admin/culture")
public class CultureController {

    @Autowired
    private CultureService cultureService;

    /**
     * 添加咖啡及文化。
     *
     * @param culture 咖啡及文化
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody Culture culture) {
//        return cultureService.save(culture);
//    }

    /**
     * 根据主键删除咖啡及文化。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return cultureService.removeById(id);
    }

    /**
     * 根据主键更新咖啡及文化。
     *
     * @param culture 咖啡及文化
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody Culture culture) {
//        return cultureService.updateById(culture);
//    }

    /**1
     * 查询所有咖啡及文化。
     *
     * @return 所有数据
     */
//    @GetMapping("list")
//    public List<Culture> list() {
//        return cultureService.list();
//    }

    /**
     * 根据咖啡及文化主键获取详细信息。
     *
     * @param id 咖啡及文化主键
     * @return 咖啡及文化详情
     */
    @GetMapping("getInfo/{id}")
    public Culture getInfo(@PathVariable Serializable id) {
        return cultureService.getById(id);
    }

    /**
     * 分页查询咖啡及文化。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Culture> page(Page<Culture> page) {
        return cultureService.page(page);
    }

    @GetMapping(value = "/list")
    public String userList(ModelMap model, @RequestParam(value = "culture", defaultValue = "") String culture,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(CultureTableDef.CULTURE.W_CONTENT.like(culture));
        Page<Culture> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<Culture> pageInfo = cultureService.page(page, queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "back/culture-list";
    }

    @GetMapping(value = "/addView")
    public String addUserView() {
        return "back/culture-add";
    }

    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Culture culture) {
        try {
            boolean r = cultureService.save(culture);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = culture.getWContent() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model) {
        Culture culture1 = cultureService.getById(id);
        model.addAttribute("culture", culture1);
        return "back/culture-edit";
    }

    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Culture culture) {
        try {
            boolean r = cultureService.updateById(culture);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = culture.getWContent() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = cultureService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}